﻿// Brandon Haynes - http://brandonhaynes.org
// Copyright (c) 2008
// by Brandon Haynes

// Please refer to the project license, located at http://www.codeplex.com/DNNLocalization/license

// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.

//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
//INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
//DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
//SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
//SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
//WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 
//USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 

using System;
using System.CodeDom;
using System.Globalization;
using System.Linq;

namespace BrandonHaynes.DotNetNukeResXBuildProvider
	{
	internal static class Extensions
		{
		public static CodeCompileUnit Adapt(this CodeCompileUnit code, ResourcePathMetadata resourceMetaData)
			{
			// Single namespace, single class in this unit
			var codeClass = code.Namespaces.Cast<CodeNamespace>().Single().Types.Cast<CodeTypeDeclaration>().Single();
			
			// Locate property "ResourceManager"
			var resourceManager = codeClass.Members.OfType<CodeMemberProperty>().Single(member => member.Name == "ResourceManager");

			// Clear existing ResourceManager_get statements
			resourceManager.GetStatements.Clear();

			// Add adapted statements to property getter:
	
				// if(resourceMan == null) 
				//		resourceMan = new BrandonHaynes.DotNetNukeResXBuildProvider.DecoratedHybridResourceManager(typeName, virtualPath); 
				resourceManager.GetStatements.Add(
					new CodeConditionStatement(
						new CodeBinaryOperatorExpression(
							new CodeVariableReferenceExpression("resourceMan"), 
							CodeBinaryOperatorType.IdentityEquality,
							new CodePrimitiveExpression(null)),
						new CodeAssignStatement(
						new CodeVariableReferenceExpression("resourceMan"),
						new CodeObjectCreateExpression("BrandonHaynes.DotNetNukeResXBuildProvider.DecoratedHybridResourceManager",
							new CodeTypeOfExpression(resourceMetaData.QualifiedTypeName),
							new CodePrimitiveExpression(resourceMetaData.VirtualPath)))));

				// return resourceMan;
				resourceManager.GetStatements.Add(
					new CodeMethodReturnStatement(
						new CodeVariableReferenceExpression("resourceMan")));

			return code;
			}

		public static bool TryGetCulture(string name, out CultureInfo culture)
			{
			try
				{
				culture = CultureInfo.GetCultureInfo(name);
				}
			catch (ArgumentException)
				{
				culture = CultureInfo.InvariantCulture;
				}

			return culture != CultureInfo.InvariantCulture;
			}
		}
	}
